package com.zhiche.lisa.lspm.service.carrier.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.core.utils.Account.AccountUtil;
import com.zhiche.lisa.lspm.dao.mapper.carrier.CarrierLicenseAttachMapper;
import com.zhiche.lisa.lspm.dao.model.carrier.CarrierLicenseAttach;
import com.zhiche.lisa.lspm.dao.model.carrier.CarrierLicenseAttachHistory;
import com.zhiche.lisa.lspm.dao.model.driver.DriverLicenseAttach;
import com.zhiche.lisa.lspm.service.carrier.ICarrierLicenseAttachHistoryService;
import com.zhiche.lisa.lspm.service.carrier.ICarrierLicenseAttachService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 牵引车证照附件 service impl
 *
 * @author lbl
 * @since 2018-05-25
 */
@Service
public class CarrierLicenseAttachServiceImpl extends ServiceImpl<CarrierLicenseAttachMapper, CarrierLicenseAttach> implements ICarrierLicenseAttachService {
    @Autowired
    private ICarrierLicenseAttachHistoryService carrierLicenseAttachHistoryService;

    @Override
    public void batchInsert(List<CarrierLicenseAttach> list) {
        this.batchInsert(list);
    }

    /**
     * 根据传入旧的附件信息进行删除
     * @param carrierLicenseAttach
     */
    @Override
    @Transactional
    public void updateOldAttachDeleteState(CarrierLicenseAttach carrierLicenseAttach) {
        EntityWrapper<CarrierLicenseAttachHistory> ewAttach = new EntityWrapper<>();
        ewAttach.eq("carrier_id", carrierLicenseAttach.getCarrierId());
        ewAttach.eq("license_id", carrierLicenseAttach.getLicenseId());
        ewAttach.eq("attach_type", carrierLicenseAttach.getAttachType());
        ewAttach.eq("attach_key", carrierLicenseAttach.getAttachKey());
        List<CarrierLicenseAttachHistory> carrierLicenseAttaches = carrierLicenseAttachHistoryService.selectList(ewAttach);
        if (CollectionUtils.isEmpty(carrierLicenseAttaches)) {
            CarrierLicenseAttachHistory carrierLicenseAttachHistor = new CarrierLicenseAttachHistory();
            BeanUtils.copyProperties(carrierLicenseAttach, carrierLicenseAttachHistor);
            carrierLicenseAttachHistor.setGmtCreate(new Date());
            carrierLicenseAttachHistor.setCreator(AccountUtil.getAccountInfoFromSecurityContext().getUsername());
            carrierLicenseAttachHistoryService.insert(carrierLicenseAttachHistor);
        }
        this.deleteById(carrierLicenseAttach.getId());
    }


}
